package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface FollowMapper {

    // 获取用户的关注列表
    @Select("SELECT u.user_id, u.user_name, u.profile_picture, u.user_sex, f.follow_time " +
            "FROM fans_tb f " +
            "JOIN user_tb u ON f.followee_id = u.user_id " +
            "WHERE f.is_unfollowed = false AND f.fan_id = #{user_id}")
    List<User> getFollowees(@Param("user_id") Integer user_id);

    // 获取用户的粉丝列表
    @Select("SELECT u.user_id, u.user_name, u.profile_picture, u.user_sex, f.follow_time " +
            "FROM fans_tb f " +
            "JOIN user_tb u ON f.fan_id = u.user_id " +
            "WHERE f.is_unfollowed = false AND f.followee_id = #{user_id}")
    List<User> getFans(@Param("user_id") Integer user_id);

    // 关注某个用户
    @Insert("INSERT INTO fans_tb(fan_id, followee_id, follow_time, is_unfollowed) " +
            "VALUES (#{fan_id}, #{followee_id}, #{follow_time}, false)")
    int followUser(@Param("fan_id") Integer fan_id, @Param("followee_id") Integer followee_id, @Param("follow_time") Date follow_time);

    // 取消关注某个用户
    @Update("UPDATE fans_tb SET is_unfollowed = true WHERE fan_id = #{fan_id} AND followee_id = #{followee_id}")
    int unfollowUser(@Param("fan_id") Integer fan_id, @Param("followee_id") Integer followee_id);

    // 检查是否已经关注过某个用户
    @Select("SELECT COUNT(*) FROM fans_tb WHERE fan_id = #{fan_id} AND followee_id = #{followee_id} AND is_unfollowed = false")
    int checkIfFollowed(@Param("fan_id") Integer fan_id, @Param("followee_id") Integer followee_id);

    // 更新关注时间
    @Update("UPDATE fans_tb SET follow_time = #{follow_time} WHERE fan_id = #{fan_id} AND followee_id = #{followee_id}")
    int updatefollow_time(@Param("fan_id") Integer fan_id, @Param("followee_id") Integer followee_id, @Param("follow_time") Date follow_time);

    // 统计某个用户的粉丝数
    @Select("SELECT COUNT(*) FROM fans_tb WHERE followee_id = #{followee_id} AND is_unfollowed = false")
    int countFollowers(@Param("followee_id") Integer followee_id);

    // 统计某个用户的关注数
    @Select("SELECT COUNT(*) FROM fans_tb WHERE fan_id = #{fan_id} AND is_unfollowed = false")
    int countFollowing(@Param("fan_id") Integer fan_id);
}
